package net.edu_soft.ec.asset.service.impl;

import net.edu_soft.ec.asset.mapper.ProductDetailMapper;
import net.edu_soft.ec.asset.pojo.ProductDetail;
import net.edu_soft.ec.asset.pojo.ProductDetailExample;
import net.edu_soft.ec.asset.service.ProductDetailService;
import net.edu_soft.ec.common.exception.OperationException;
import net.edu_soft.ec.common.util.PuingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductDetailServiceImpl implements ProductDetailService {

    @Autowired
    private ProductDetailMapper detailMapper;

    @Override
    public ProductDetail getDetailByProductId(int productId) {
        ProductDetail productDetail = null;
        ProductDetailExample exampmle = new ProductDetailExample();
        ProductDetailExample.Criteria criteria = exampmle.createCriteria();
        //添加产品id
        criteria.andProductIdEqualTo(productId);
        List<ProductDetail> detailList = detailMapper.selectByExample(exampmle);
        if (detailList.size() > 0) {
            //提取第一个元素
            productDetail = detailList.get(0);
        }
        return productDetail;
    }

    @Override
    public int saveDetail(ProductDetail productDetail) {
        //校验数据
        checkData(productDetail);
        return detailMapper.insertSelective(productDetail);
    }

    @Override
    public int updateDetail(ProductDetail productDetail) {
        //用产品详情id查询该产品 详情 无则抛出异常
        selDetailById(productDetail.getProductDetailId());
        //校验数据
        checkData(productDetail);
        return detailMapper.updateByPrimaryKeySelective(productDetail);
    }

    /***
     * 校验产品详情
     * @param detail 产品详情
     */
    private void checkData(ProductDetail detail){
        //校验产品id
        PuingUtil.notNullByZero(detail.getProductId(),"产品id不能为空或小于等于0");
        //校验 设计语言、支持系统、简介、功能、说明书，使用手册、配置信息、文件
        PuingUtil.notNullString(detail.getSysLanguage(),detail.getSupportSys(),detail.getIntroduce()
                ,detail.getFunctions(),detail.getManual(), detail.getConfigInfo(),detail.getProFile());

    }

    /***
     * 根据详情id查询产品详情
     * @param detailId 产品详情id
     * @return 产品信息
     */
    private void selDetailById(int detailId){
        ProductDetail detail = detailMapper.selectByPrimaryKey(detailId);
        if(detail==null){
            throw new OperationException("该id查不到该详情");
        }
    }


}
